/*
 * @Author: 葛文杰
 * @Date: 2021/1/29 15:46
 * @Description: egg proxy 枚举数据
 */
const { BASE_WHERE_CONDITION_SQL } = require('../util/common');

const TABLE_NAME = 'enumeration_info'; // 表名

const TABLE_FIELD = 'id,enumerationType,enumerationTypeName,enumerationValue,enumerationName'; // 字段

module.exports = {

    /**
     * 获取枚举数据列表
     */
    async getEnumerationList(conn, enumerationName, enumerationType, pageNo, pageSize) {
        let start = (pageNo - 1) * pageSize;
        let selectSql =
          `select ${TABLE_FIELD} from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        if (enumerationType) {
            conditionSql = `${conditionSql} and enumerationType = ?`;
            params.push(enumerationType);
        }
        if (enumerationName) {
            conditionSql = `${conditionSql} and enumerationName like concat('%',?,'%')`;
            params.push(enumerationName);
        }
        params = [ ...params, start, pageSize ];
        let sql =
          `${selectSql} ${conditionSql} limit ?,?`;
        let enumerationList = await conn.query(sql, params);
        return enumerationList;
    },

    /**
     * 获取数据源总数
     */
    async getEnumerationTotal(conn, enumerationName, enumerationType) {
        let selectSql = `select count(1) as count from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        if (enumerationType) {
            conditionSql = `${conditionSql} and enumerationType = ?`;
            params.push(enumerationType);
        }
        if (enumerationName) {
            conditionSql = `${conditionSql} and enumerationName like concat('%',?,'%')`;
            params.push(enumerationName);
        }
        let sql = `${selectSql} ${conditionSql}`;
        let [ total ] = await conn.query(sql, params);
        let { count } = total;
        return count;
    },

    /**
     * 校验是否存在相同枚举数据
     */
    // TODO 技能有问题
    async judgeIsExistSameEnumeration(
      conn,
      enumerationId,
      enumerationInfo
    ) {
        let {
            enumerationType,
            enumerationName,
            enumerationValue
        } = enumerationInfo;
        let selectSql = `select count(1) as count from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        if (enumerationId) {
            conditionSql = `${conditionSql} and id != ?`;
            params.push(enumerationId);
        }
        conditionSql = `${conditionSql} and enumerationType = ? and (enumerationName = ? or enumerationValue = ?)`;
        params.push(enumerationType, enumerationName, enumerationValue);
        let sql = `${selectSql} ${conditionSql}`;
        let [ total ] = await conn.query(sql, params);
        let { count } = total;
        let isExist = count > 0;
        return isExist;
    },

    /**
     * 根据类型获取枚举数据数组
     * @return {Promise<void>}
     */
    async getEnumerationListByCode(conn, code) {
        let selectSql = `select id as value, enumerationName as label from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        if (code) {
            conditionSql = `${conditionSql} and enumerationType = ?`;
            params.push(code);
        }
        let sql = `${selectSql} ${conditionSql}`;
        let enumerationArray = await conn.query(sql, params);
        return enumerationArray;
    },

    /**
     * 根据类型获取枚举数据信息
     * @return {Promise<void>}
     */
    async getEnumerationInfoById(conn, enumerationId) {
        let selectSql = `select id, enumerationValue, enumerationName from ${TABLE_NAME}`;
        let conditionSql = `${BASE_WHERE_CONDITION_SQL}`;
        let params = [];
        if (enumerationId) {
            conditionSql = `${conditionSql} and id = ?`;
            params.push(enumerationId);
        }
        let sql = `${selectSql} ${conditionSql}`;
        let enumerationInfo = await conn.query(sql, params);
        return enumerationInfo;
    },

    /**
     * 通过枚举值获取枚举信息
     * @param conn
     * @param condition
     * @return {Promise<void>}
     */
    async getEnumerationInfoByValue(conn, condition) {
        let result = await conn.get(TABLE_NAME, condition);
        return result;
    },

    /**
     * 保存枚举数据信息
     * @return {Promise<*>}
     */
    async saveEnumerationInfo(conn, enumerationInfo) {
        let result = await conn.insert(TABLE_NAME, enumerationInfo);
        return result;
    },

    /**
     * 修改枚举数据信息
     */
    async updateEnumerationInfoById(conn, enumerationId, enumerationInfo) {
        let result = await conn.update(TABLE_NAME, enumerationInfo);
        return result;
    },


    /**
     * 通过id删除枚举数据
     * @return {Promise<void>}
     */
    async deleteEnumerationById(conn, condition) {
        let result = await conn.delete(TABLE_NAME, condition);
        return result;
    }
};
